#include"offlinemessagemodel.hpp"
#include"ConnectionPool.h"


void OfflineMsgModel::insert(int userid, string msg)
{
    char sql[1024] = {0};
    sprintf(sql,"insert into OfflineMessage(userid,message) values(%d,'%s')",
        userid,msg.c_str());

    ConnectionPool *cp = ConnectionPool::getConnectionPool();
    shared_ptr<Connection> sp = cp->getConnection();

    sp->update(sql);

}


void OfflineMsgModel::remove(int userid)
{
    char sql[1024] = {0};
    sprintf(sql,"delete from OfflineMessage where userid=%d",
        userid);

    ConnectionPool *cp = ConnectionPool::getConnectionPool();
    shared_ptr<Connection> sp = cp->getConnection();
    sp->update(sql);

}

vector<string> OfflineMsgModel::query(int userid)
{
     // 组装sql语句
    char sql[1024] = {0};
    sprintf(sql,"select message from OfflineMessage where userid = %d",userid);

    vector<string> vec;

    ConnectionPool *cp = ConnectionPool::getConnectionPool();
    shared_ptr<Connection> sp = cp->getConnection();

    MYSQL_RES *res = sp->query(sql);
    if(res != nullptr)
    {
        // 获取userid的所有消息
        MYSQL_ROW row;
        while((row=mysql_fetch_row(res)) != nullptr)
        {
            vec.push_back(row[0]);
        }
        mysql_free_result(res);
        return vec;
    }


    return vec;
}
